﻿package domino 
{

	/**
	 * ...
	 * @author ...
	 */
	import flash.display.MovieClip;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.display.Sprite;
	
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	
	
	public class actorCoin extends actor
	{
	//	private static const BALL_D:int = 32;
		
	  [Embed(source="img/star.svg")]
           public var starSVG:Class;
	  [Embed(source="img/star2.svg")]
           public var starSVG2:Class;	   
		   
	    public var stat:Number;
		public var circle:Sprite;
	
		public var scaleX:Number, scaleY:Number;
		public var density:Number, angV:Number;
		public var lineStar:Sprite = new Sprite();
		public var taked:Boolean = false;
		public var friction:Number = 0;
	//	public var select:Boolean = false;
		public function actorCoin(_world:b2World,parent:DisplayObjectContainer,dominPosition:Point,_rot:Number,_density:Number,_friction:Number,_scaleX:Number,_scaleY:Number,_restitution:Number) 
		{ 
			//costume
			friction = _friction;
			if(_friction)
		     circle =  new starSVG()
			else
			 circle =  new starSVG2()
			
			 //circle = new starSVG();
			circle.scaleX = _scaleX;
			circle.scaleY = _scaleX;
			scaleX = _scaleX;
			scaleY = _scaleX;
			circle.x = dominPosition.x;
			circle.y = dominPosition.y;
			circle.rotation = _rot;
			
			//linestar
			lineStar.graphics.clear();
			if(_friction!=0)
             lineStar.graphics.lineStyle(0,0xFF0000,0.2);
			else
			  lineStar.graphics.lineStyle(0,0x000033,0.2);
			
            //lineStar.graphics.beginFill(0xFF0000,0.5)
            lineStar.graphics.moveTo(dominPosition.x, dominPosition.y); 
		    lineStar.graphics.lineTo(dominPosition.x,dominPosition.y-Math.random()*200-30);
		    lineStar.visible = true;
		    //lineStar.graphics.endFill();
            
			parent.addChild(lineStar);
			parent.addChild(circle);
			
			//body
			var final_body:b2Body;
			var the_circle:b2CircleDef = new b2CircleDef() ;
			
			the_circle.radius = circle.width / 60; ;
			the_circle.friction=_friction;
			the_circle.density = _density;
			the_circle.restitution = _restitution;
			the_circle.isSensor = true;
			density = _density;
			angV = 0;
			
			var the_body:b2BodyDef = new b2BodyDef();
			the_body.position.Set(circle.x / 30, circle.y / 30);
			the_body.angle = _rot * Math.PI / 180;
			
			final_body=_world.CreateBody(the_body);
			final_body.CreateShape(the_circle);
			final_body.AllowSleeping(true);
			final_body.SetMassFromShapes();
		
			super(final_body, circle);
			//parent.addEventListener(takeCoin.TAKE_COIN,coinHandler);
			//setMovie(_density) ;
        }//end
		
	 public function removeLine(parent:DisplayObjectContainer):void {
			parent.removeChild(lineStar);
			}//end
	 public function getData():Array {
		var data:Array = new Array();
		data.push(Number(_costume.x));
		data.push(Number(_costume.y));
		data.push(Number(circle.scaleX));
		data.push(Number(circle.scaleY));
		data.push(Number(_body.GetShapeList().GetFriction()));
		data.push(Number(_body.GetShapeList().m_density));
		data.push(Number(_costume.rotation));
		data.push(Number(_body.GetShapeList().GetRestitution()));
		data.push(0);
		return data;
	}
		
	public function takeMe():void {
		if (taked == false) {
			if (friction != 0)
			 dispatchEvent(new takeCoin(takeCoin.TAKE_COIN));
			else 
			 dispatchEvent(new takeCoin(takeCoin.TAKE_COIN_BLACK));
		 taked = true;
		}
		else 
		{trace("taked yet"); }
	}
		
        
	private function setMovie(s:Number):void {
			if(s==0)
			 MovieClip(_costume).gotoAndStop(2);
			else
			 MovieClip(_costume).gotoAndStop(1);
			 
			}
	}
     
}